﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Automator.Classes;
using Automator.Runtime;
using Automator.StartPage;
using Automator.Tasks.Core;
using AvalonDock;

namespace Automator
{
    public partial class MainWindow: Window
    {       
        /// <summary>
        /// Creates a WorkflowDocument (a visual frontend) based on provided workflow
        /// </summary>
        /// <param name="workflow">Base workflow</param>
        private WorkflowDocument AddWorkflowDocument(Workflow workflow)
        {
            foreach (DocumentContent doc in dockingManager.Documents)
            {
                if (doc is WorkflowDocument && (doc as WorkflowDocument).WorkflowInstance.Equals(workflow))
                {
                    doc.Activate();
                    return doc as WorkflowDocument;
                }
            }
            
            WorkflowDocument document = new WorkflowDocument() { IsFloatingAllowed = false, WorkflowInstance = workflow,
                Title = workflow.Name };
            DockPanel dock = new DockPanel() { LastChildFill = true };
            TreeView tree = new TreeView() { Focusable = true, AllowDrop = true,
                ItemTemplate = this.Resources["workflowTreeTemplate"] as HierarchicalDataTemplate,
                ItemContainerStyle = this.Resources["workflowTreeItemStyle"] as Style };
            document.TasksTree = tree;
            tree.ItemsSource = workflow.Tasks;
            tree.PreviewDrop += treeWorkflow_Drop;            
            dock.Children.Add(tree);
            document.Content = dock;
            
            workflow.ModifiedChanged += delegate { document.Title = workflow.Name; };
            workflow.Log.LogItemAdded += delegate {
                lstOutput.SelectedIndex = lstOutput.Items.Count - 1;
                lstOutput.ScrollIntoView(lstOutput.SelectedItem);
            };
            document.TasksTree.MouseDoubleClick += delegate(object sender, MouseButtonEventArgs e)
            {
                e.Handled = true;
                Task task = document.TasksTree.SelectedItem as Task;
                if (task != null) 
                {
                    if (task.EditTask())
                        workflow.Modified = true;
                }
            };
            
            document.Show(dockingManager);
            document.Activate();
            return document;
        }
        
        /// <summary>
        /// Creates a StartPageDocument (a visual frontend) for Automator Start Page
        /// </summary>
        private StartPageDocument AddStartPageDocument()
        {
            foreach (DocumentContent doc in dockingManager.Documents)
            {
                if (doc is StartPageDocument)
                {
                    doc.Activate();
                    return doc as StartPageDocument;
                }
            }
            
            StartPageDocument document = new StartPageDocument() { IsFloatingAllowed = false, Title = "Start Page" };
            StartPageContent content = new StartPageContent();
            content.NewSolutionLink.Command = AutomatorCommands.NewSolution;
            content.NewWorkflowLink.Command = AutomatorCommands.NewWorkflow;
            content.OpenSolutionLink.Command = AutomatorCommands.OpenSolution;
            content.OpenWorkflowLink.Command = AutomatorCommands.OpenWorkflow;
            content.SqlServerLink.Command = AutomatorCommands.SqlSettings;
            content.DocumentationLink.Command = AutomatorCommands.OnlineDocumentation;
            document.Content = content;
            
            document.Show(dockingManager);
            document.Activate();
            return document;
        }
        
        private void DockingManager_ActiveDocumentChanged(object sender, EventArgs e)
        {
            if (lstOutput != null)
            {
                if (cmbOutputTargets.SelectedValue.Equals("Solution"))
                    lstOutput.ItemsSource = solution.Log.Instance;
                else if (cmbOutputTargets.SelectedValue.Equals("Workflow") &&
                    dockingManager.ActiveDocument is WorkflowDocument)
                    lstOutput.ItemsSource = (dockingManager.ActiveDocument as WorkflowDocument).WorkflowInstance.Log.Instance;
                else
                    lstOutput.ItemsSource = null;
                
                //lstOutput.SelectedIndex = lstOutput.Items.Count - 1;
                if (lstOutput.Items.Count > 0)
                    lstOutput.ScrollIntoView(lstOutput.Items[lstOutput.Items.Count - 1]);
            }
        }
    }
}
